# Dinghong Cloud Screen Project Minikube Deployment Script (Windows PowerShell)
# Based on Docker, Minikube and Nginx Ingress

param(
    [switch]$SkipBuild
)

$ErrorActionPreference = "Stop"

Write-Host "Starting to deploy Dinghong Cloud Screen project to Minikube..." -ForegroundColor Green

# Check Minikube status
$minikubeStatus = minikube status
if ($minikubeStatus -notmatch "Running") {
    Write-Host "Starting Minikube..." -ForegroundColor Yellow
    minikube start --cpus=4 --memory=8192 --disk-size=20g
} else {
    Write-Host "Minikube is already running" -ForegroundColor Green
}

# Enable Ingress addon
Write-Host "Enabling Ingress addon..." -ForegroundColor Yellow
minikube addons enable ingress

# Wait for Ingress controller to be ready
Write-Host "Waiting for Ingress controller to be ready..." -ForegroundColor Yellow
kubectl wait --namespace ingress-nginx --for=condition=ready pod --selector=app.kubernetes.io/component=controller --timeout=120s

# Get script directory
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path

# Apply Kubernetes configurations
Write-Host "Applying Kubernetes configurations..." -ForegroundColor Yellow

# Create namespace
kubectl apply -f "$SCRIPT_DIR\namespace.yaml"

# Deploy database
Write-Host "Deploying MySQL..." -ForegroundColor Yellow
kubectl apply -f "$SCRIPT_DIR\mysql-deployment.yaml"

# Deploy Redis
Write-Host "Deploying Redis..." -ForegroundColor Yellow
kubectl apply -f "$SCRIPT_DIR\redis-deployment.yaml"

# Wait for databases to be ready
Write-Host "Waiting for databases to be ready..." -ForegroundColor Yellow
kubectl wait --namespace dinghong-cloud-screen --for=condition=ready pod --selector=app=gva-mysql --timeout=300s
kubectl wait --namespace dinghong-cloud-screen --for=condition=ready pod --selector=app=gva-redis --timeout=300s

# Deploy backend service
Write-Host "Deploying backend service..." -ForegroundColor Yellow
kubectl apply -f "$SCRIPT_DIR\configmap-server.yaml"
kubectl apply -f "$SCRIPT_DIR\server-deployment.yaml"

# Deploy frontend service
Write-Host "Deploying frontend service..." -ForegroundColor Yellow
kubectl apply -f "$SCRIPT_DIR\nginx-config.yaml"
kubectl apply -f "$SCRIPT_DIR\web-deployment.yaml"

# Deploy Ingress
Write-Host "Deploying Ingress..." -ForegroundColor Yellow
kubectl apply -f "$SCRIPT_DIR\nginx-ingress.yaml"

# Wait for all pods to be ready
Write-Host "Waiting for all services to be ready..." -ForegroundColor Yellow
kubectl wait --namespace dinghong-cloud-screen --for=condition=ready pod --selector=app=gva-server --timeout=300s
kubectl wait --namespace dinghong-cloud-screen --for=condition=ready pod --selector=app=gva-web --timeout=300s

# Get access information
Write-Host "Getting access information..." -ForegroundColor Yellow
$MINIKUBE_IP = minikube ip
Write-Host "Minikube IP: $MINIKUBE_IP" -ForegroundColor Cyan

# Add local hosts record
Write-Host "Adding local hosts record..." -ForegroundColor Yellow
$hostsPath = "$env:SystemRoot\System32\drivers\etc\hosts"
$hostsContent = Get-Content $hostsPath -Raw
if ($hostsContent -notmatch "dinghong-cloud-screen\.local") {
    Add-Content -Path $hostsPath -Value "`n$MINIKUBE_IP dinghong-cloud-screen.local" -Force
    Write-Host "Added hosts record: $MINIKUBE_IP dinghong-cloud-screen.local" -ForegroundColor Green
} else {
    Write-Host "Hosts record already exists" -ForegroundColor Yellow
}

# Show service status
Write-Host "Showing service status..." -ForegroundColor Yellow
kubectl get pods -n dinghong-cloud-screen
kubectl get services -n dinghong-cloud-screen
kubectl get ingress -n dinghong-cloud-screen

Write-Host ""
Write-Host "Deployment completed!" -ForegroundColor Green
Write-Host "Access URL: http://dinghong-cloud-screen.local" -ForegroundColor Cyan
Write-Host "Or: http://$MINIKUBE_IP" -ForegroundColor Cyan
Write-Host ""
Write-Host "If you cannot access, please run the following command to enable tunnel:" -ForegroundColor Yellow
Write-Host "minikube tunnel" -ForegroundColor White
Write-Host ""
Write-Host "View logs:" -ForegroundColor Yellow
Write-Host "kubectl logs -f deployment/gva-server -n dinghong-cloud-screen" -ForegroundColor White
Write-Host "kubectl logs -f deployment/gva-web -n dinghong-cloud-screen" -ForegroundColor White 